//
//  BudgetEditeSectionView.m
//  BaseProject
//
//  Created by mac on 2020/10/27.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BudgetEditeSectionView.h"
#import "BudgetPieItemModel.h"
@interface BudgetEditeSectionView ()
@property(nonatomic,strong)UILabel *nameLab;
@property(nonatomic,strong)NSMutableArray<UILabel *> *itemsArray;
@end

@implementation BudgetEditeSectionView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    self.backgroundColor = [UIColor whiteColor];
}
- (void)createUI
{
    
}
#pragma mark - setter
- (void)setIconModel:(BudgetPieItemModel *)iconModel
{
    _iconModel = iconModel;
    //清空
    [self.itemsArray enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray removeAllObjects];
    self.nameLab.text = iconModel.title;
    CGFloat orinY = 0,itemHeight = 32*ADAPTER_WIDTH,orinXMargin = 16*ADAPTER_WIDTH,itemMargin = 12*ADAPTER_WIDTH,itemWidth = (self.width - orinXMargin * 2 - itemMargin * 2) / 3;
    for (NSInteger index = 0; index < iconModel.dataSource.count; index ++) {
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(orinXMargin + (itemWidth + itemMargin) * (index % 3), 40*ADAPTER_WIDTH + (itemHeight + itemMargin) * (index / 3), itemWidth, itemHeight)];
        [self addSubview:lab];
        lab.layer.cornerRadius = itemHeight * 0.5;
        lab.layer.masksToBounds = YES;
        if (iconModel.dataSource[index].isSelected) {
            lab.backgroundColor = RGB(255, 83, 76);
            lab.textColor = RGB(255, 255, 255);
        } else {
            lab.backgroundColor = RGB(235, 235, 235);
            lab.textColor = RGB(102, 102, 102);
        }
        lab.text = iconModel.dataSource[index].title;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        
        orinY = lab.bottom + itemMargin;
        [self.itemsArray addObject:lab];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItemLab:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
        lab.tag = index;
    }
    self.height = orinY;
}
#pragma mark - click
- (void)clickItemLab:(UITapGestureRecognizer *)tap
{
    BudgetPieItemModel *itemModel = self.iconModel.dataSource[tap.view.tag];
    itemModel.isSelected = !itemModel.isSelected;
    
    UILabel *lab = (UILabel *)tap.view;
    if (itemModel.isSelected) {
        lab.backgroundColor = RGB(255, 83, 76);
        lab.textColor = RGB(255, 255, 255);
    } else {
        lab.backgroundColor = RGB(235, 235, 235);
        lab.textColor = RGB(102, 102, 102);
    }
    
    [self realTimeCallBack];
}
#pragma mark - 回调
- (void)realTimeCallBack
{
    !self.realTimeCallBackBlock ?: self.realTimeCallBackBlock ();
}
#pragma mark - lazy懒加载
- (UILabel *)nameLab
{
    if (!_nameLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = RGB(102, 102, 102);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(16*ADAPTER_WIDTH, 15*ADAPTER_WIDTH, self.width - 32*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        _nameLab = lab;
    }
    return _nameLab;
}
- (NSMutableArray<UILabel *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
@end
